package com.chenyu.upload.schedule;

import com.chenyu.upload.dao.PictureMapper;
import com.chenyu.upload.entity.Picture;
import com.chenyu.upload.util.QiniuOperateUtil;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang3.RandomUtils;
import org.springframework.scheduling.annotation.Scheduled;
import org.springframework.stereotype.Component;

import javax.annotation.Resource;
import java.io.File;
import java.util.Arrays;
import java.util.Collections;
import java.util.List;
import java.util.Optional;

/**
 * @author yu_chen
 * @date 2018年10月19日14:21:44
 */
@Slf4j
@Component
public class QuartzService {


    private static final String PATH = "C:\\随机头像";

    private QiniuOperateUtil qiniuOperateUtil = new QiniuOperateUtil();

    @Resource
    private PictureMapper pictureMapper;

    /**
     *
     */
//    @Scheduled(fixedRate = 10)
    public void insertPicture() {
        File file = new File(PATH);
        File[] files = file.listFiles();
        List<File> files1 = Arrays.asList(files);
        //打乱文件顺序
        Collections.shuffle(files1);
        List<File> files2 = files1.subList(0, RandomUtils.nextInt(200,300));
        StringBuilder sb = new StringBuilder();
        for (File file1 : files2) {
            String uuid = System.currentTimeMillis() + "" + RandomUtils.nextLong(100000000, 999999999);
            qiniuOperateUtil.uploadFileToPrivateSpaceFile(file1, uuid + ".jpg");
            sb.append(uuid).append(".jpg%2C");
        }
        Picture picture = new Picture(sb.toString());
        pictureMapper.insert(picture);
        System.out.println("插入图片数量：" + picture.getId());
    }

}
